package cn.bb.bbstudy.controller;

import cn.bb.bbstudy.pojo.Blog;
import cn.bb.bbstudy.pojo.BlogCategory;
import cn.bb.bbstudy.service.BlogCategoryService;
import cn.bb.bbstudy.service.BlogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class BlogCategoryContrlller {

    @Autowired
    BlogCategoryService blogCategoryService;
    @Autowired
    BlogService blogService;

    @GetMapping("/blog/category")
    //默认为15  数据表里的第一条
    public String listCategories(Model model,@RequestParam(defaultValue = "15") int cid,
                                 @RequestParam(defaultValue = "1") int page,
                                 @RequestParam(defaultValue = "10") int limit) {
        PageHelper.startPage(page,limit);
        //根据分类名 查询问题信息 降序排列
        PageInfo<Blog> info = new PageInfo<>(blogService.findAllByCIdDesc(cid));
        model.addAttribute("info", info);
        //需要该分类信息和全部分类信息  前者是右上角筛选用的 后者是为了防止报错
        BlogCategory category = blogCategoryService.selectByPrimaryKey(cid);
        model.addAttribute("thisCategoryName", category.getCategory());
        //先不写 看会不会报错  实验 不会报错 但标签没了 需要补上
        List<BlogCategory> categoryList =   blogCategoryService.selectAll();
        model.addAttribute("categoryList", categoryList);
        return "blog/list";
    }
}
